package com.ayuku.retail.ui.category;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.ayuku.commons.Util;
import com.ayuku.retail.business.stock.StockImpl;
import com.ayuku.retail.persistence.model.Stock;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class CategoryLookup extends org.eclipse.swt.widgets.Dialog {

	private Shell dialogShell;
	private Table tableData;
	private Label label1;
	private Label label2;
	private TableColumn clmHarga;
	private TableColumn clmDisc;
	private TableColumn clmCategory;
	private TableColumn clmStock;
	private TableColumn clmNama;
	private TableColumn clmKode;
	private Text txtSearch;

	/**
	 * Auto-generated main method to display this org.eclipse.swt.widgets.Dialog
	 * inside a new Shell.
	 */
	public static void main(String[] args) {
		try {
			Display display = Display.getDefault();
			Shell shell = new Shell(display);
			CategoryLookup inst = new CategoryLookup(shell, SWT.NULL);
			inst.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public CategoryLookup(Shell parent, int style) {
		super(parent, style);
	}

	public void open() {
		try {

			Shell parent = getParent();
			dialogShell = new Shell(parent, SWT.DIALOG_TRIM
					| SWT.APPLICATION_MODAL);

			dialogShell.setLayout(new FormLayout());
			dialogShell.setBackground(SWTResourceManager.getColor(192, 192, 192));

			label2 = new Label(dialogShell, SWT.NONE);
			FormData label2LData = new FormData();
			label2LData.left =  new FormAttachment(0, 1000, 6);
			label2LData.width = 612;
			label2LData.height = 16;
			label2LData.bottom =  new FormAttachment(1000, 1000, -5);
			label2LData.right =  new FormAttachment(1000, 1000, -22);
			label2.setLayoutData(label2LData);
			label2.setText("F3 untuk pindah focus.   Enter untuk confirm");
			label2.setFont(SWTResourceManager.getFont("Courier", 10, 0, false, false));
			label2.setBackground(SWTResourceManager.getColor(192, 192, 192));

			label1 = new Label(dialogShell, SWT.NONE);
			FormData label1LData = new FormData();
			label1LData.left = new FormAttachment(0, 1000, 12);
			label1LData.top = new FormAttachment(0, 1000, 12);
			label1LData.width = 62;
			label1LData.height = 21;
			label1.setLayoutData(label1LData);
			label1.setText("Search: ");
			label1.setAlignment(SWT.RIGHT);
			label1.setFont(SWTResourceManager.getFont("Courier", 12, 0, false,
					false));
			label1.setBackground(SWTResourceManager.getColor(192, 192, 192));

			txtSearch = new Text(dialogShell, SWT.SEARCH | SWT.BORDER);
			FormData text1LData = new FormData();
			text1LData.left =  new FormAttachment(0, 1000, 80);
			text1LData.top =  new FormAttachment(0, 1000, 8);
			text1LData.width = 546;
			text1LData.height = 19;
			text1LData.right =  new FormAttachment(1000, 1000, -10);
			txtSearch.setLayoutData(text1LData);
			txtSearch.setFont(SWTResourceManager.getFont("Courier", 12, 0,
					false, false));
			txtSearch.addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent evt) {
					txtSearchKeyPressed(evt);
				}
			});

			tableData = new Table(dialogShell, SWT.VIRTUAL | SWT.SINGLE
					| SWT.FULL_SELECTION | SWT.BORDER | SWT.V_SCROLL
					| SWT.H_SCROLL);
			FormData table1LData = new FormData();
			table1LData.left =  new FormAttachment(0, 1000, 4);
			table1LData.top =  new FormAttachment(0, 1000, 39);
			table1LData.width = 611;
			table1LData.height = 268;
			table1LData.right =  new FormAttachment(1000, 1000, -4);
			table1LData.bottom =  new FormAttachment(1000, 1000, -29);
			tableData.setLayoutData(table1LData);
			tableData.setHeaderVisible(true);
			tableData.setLinesVisible(true);

			tableData.setFont(SWTResourceManager.getFont("Courier", 12, 0, false, false));
			tableData.addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent evt) {
					table1KeyPressed(evt);
				}
			});
			tableData.addMouseListener(new MouseAdapter() {
				public void mouseDoubleClick(MouseEvent evt) {
					table1MouseDoubleClick(evt);
				}
			});

			clmKode = new TableColumn(tableData, SWT.NONE);
			clmKode.setText("Kode");
			clmKode.setWidth(140);

			clmNama = new TableColumn(tableData, SWT.NONE);
			clmNama.setText("Nama");
			clmNama.setWidth(240);

			clmHarga = new TableColumn(tableData, SWT.NONE);
			clmHarga.setText("Harga");
			clmHarga.setAlignment(SWT.RIGHT);
			clmHarga.setWidth(110);

			clmDisc = new TableColumn(tableData, SWT.NONE);
			clmDisc.setText("Disc (%)");
			clmDisc.setAlignment(SWT.RIGHT);
			clmDisc.setWidth(80);

			
			clmStock = new TableColumn(tableData, SWT.NONE);
			clmStock.setText("Jumlah");
			clmStock.setAlignment(SWT.RIGHT);
			clmStock.setWidth(100);

			clmCategory = new TableColumn(tableData, SWT.NONE);
			clmCategory.setText("Category");
			clmCategory.setWidth(103);

			loadData("%%");
			dialogShell.layout();

			// dialogShell.setLocation(getParent().toDisplay(100, 100));
			Util.setSize(dialogShell, 748, 450);
			Util.setCenter(dialogShell);
			// dialogShell.pack();
			dialogShell.open();

			tableData.setFocus();
			
			if (tableData.getItemCount()>0){
				tableData.setSelection(0);
			}else{
				txtSearch.setFocus();
			}
			
			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}

			// pack();
			// pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void loadData(String arg) throws Exception {
		List<Stock> lst = StockImpl.getListForLookup("%" + arg + "%");

		tableData.setItemCount(lst.size());
		for (int j = 0; j < lst.size(); j++) {
			final TableItem item = tableData.getItem(j);
			Stock pj = lst.get(j);
			item.setText(0, Util.nullToString(pj.getBarcode()));
			item.setText(1, Util.nullToString(pj.getName()));
			item.setText(2, Util.formatToNumber(pj.getSellingPrice()));
			item.setText(3, Util.formatToNumber(pj.getDiscount()));
			item.setText(4, Util.nullToString(pj.getQtyOnHand()));
			item.setText(5, Util.nullToString(pj.getCategoryCode()));

		}
	}

	private void txtSearchKeyPressed(KeyEvent evt) {
		if (evt.keyCode == SWT.CR) {
			try {
				loadData(txtSearch.getText().trim());
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (evt.keyCode == SWT.F3) {
			tableData.setFocus();
			if (tableData.getItemCount() > 0) {
				
				tableData.setSelection(0);
			}			
		}
	}

	public void setForm(TableItem itm) {
	}

	
	private void confirmSelect(){
		TableItem[] selection = tableData.getSelection();
		if ((selection != null) && (selection.length > 0)) {
			TableItem itm = (TableItem) selection[0];
			setForm(itm);
		} else {
			Util.showMsgBox("Silakan terlebih dulu pilih data yg akan diedit!",
					0, dialogShell);
		}
		dialogShell.close();
	}
	private void table1MouseDoubleClick(MouseEvent evt) {
		confirmSelect();
	}

	private void table1KeyPressed(KeyEvent evt) {
		if (evt.keyCode==SWT.CR){
			confirmSelect();
		}else if (evt.keyCode == SWT.F3) {
			txtSearch.setFocus();
		}
	}

}
